#!/usr/bin/perl

my $maxlines = $ARGV[0] || 0;
my $offset = $ARGV[1] || 0;

if ($maxlines == 0) {
	print STDERR "Usage: subcorpus <maxlines> [ <offset> ]\n";
	return 1;
}
print STDERR "Extracting the first $maxlines" . "k lines of text until empty line from stdin to stdout (with offset $offset" . "k)\n";
$maxlines += $offset;
my $lines = 0;
my $extract = $offset == 0;
while (my $line = <STDIN>) {
	# Print current line if we are extracting
	++$lines;
	if ($extract) {
		print $line;
	}

	# Stop extraction at first blank line after $maxlines has been
	# exceeded
	if ($extract && $lines > $maxlines * 1000 && $line =~ /^\s*$/) {
		last;
	}

	# Start extraction after the first blank line after $offset lines 
	if (! $extract && $lines >= $offset * 1000 && $line =~ /^\s*$/) {
		$extract = 1;
	}
}

